<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\8\21 0021
 * Time: 11:04
 */
namespace jingshan;

class PdoMiao
{
    protected $config = array(
        'hostname' => 'rm-uf686qq9th9xw4a15o.mysql.rds.aliyuncs.com',//阿里云数据库地址
//        'hostname' => '127.0.0.1',
        'database' => 'jingshan',
        'hostport' => '3306',
        'charset' => 'utf8',
    );
    protected $dbn;
    protected $user = 'jingshan';
    protected $pwd = 'flzx_3QC';
    //远程连接
    public function __construct()
    {
        $dsn = $this->parseDsn($this->config);
        try
        {
            $this->dbn = new \PDO($dsn,$this->user,$this->pwd);
        }catch (\PDOException $e)
        {
            echo 'Connection failed:'.$e->getMessage();
        }
    }
    //dsn组装
    protected function parseDsn($config)
    {
        $dsn = 'mysql:dbname='.$config['database'].';host='.$config['hostname'];
        if (!empty($config['hostport']))
        {
            $dsn .= ';port='.$config['hostport'];
        }
        if (!empty($config['charset']))
        {
            $dsn .= ';charset='.$config['charset'];
        }
        return $dsn;
    }
    //插入数据到数据库
    public function insert($datas)
    {
        $sql = "INSERT INTO `miaosha` (`id`,`phone`,`number`) VALUES (NULL,NULL,NULL)";
        $sth = $this->dbn->prepare($sql);
        foreach ($datas as $k => $v)
        {
            $sql2 = "SELECT * FROM `miaosha` WHERE `phone` = '".$v[0]."' AND `number`='".$v[1]."'";
            $sth2 = $this->dbn->prepare($sql2);
            $sth2->execute();
            $rr2 = $sth2->fetchAll();
            if (empty($rr2))
            {
//                echo "执行插入";
//                var_dump($v);
                $a = $sth->execute($v);
//                var_dump($a);
            }
        }
    }

    public function select()
    {
        $sql = "SELECT * FROM `miaosha`";
        $sth2 = $this->dbn->prepare($sql);
        $sth2->execute();
        $rr2 = $sth2->fetchAll();
        return $rr2;
    }
}